	
	package filter;
	
	import java.io.IOException;
	import javax.servlet.Filter;
	import javax.servlet.FilterChain;
	import javax.servlet.FilterConfig;
	import javax.servlet.ServletException;
	import javax.servlet.ServletRequest;
	import javax.servlet.ServletResponse;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	import javax.servlet.http.HttpSession;
	
	public class AccessFilter implements Filter {
	
		public void destroy() {
		}
	
		public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
				throws IOException, ServletException {
			
			//造型
			HttpServletRequest request = (HttpServletRequest) servletRequest;
			HttpServletResponse response = (HttpServletResponse) servletResponse;
			//获取地址
			String currentURL = request.getRequestURI();
			String targetURL = currentURL.substring(currentURL.indexOf("/", 1));
			HttpSession session = request.getSession();
			//若不为登录页则对session进行验证
			if (!"/index.jsp".equals(targetURL)) {
				if (session == null || session.getAttribute("username") == null) {
					response.sendRedirect(request.getContextPath() + "/index.jsp");
					return;
				}
			}
			//下传
			chain.doFilter(servletRequest, servletResponse);
			
		}
	
		public void init(FilterConfig fConfig) throws ServletException {
		}
	
	}
